import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutterapp1/Base/BaseWidget/BaseWidget.dart';
import 'ATBookListWidget.dart';
import 'color/MyColor.dart';
import 'model/ATClassify.dart';
import 'view/ATClassifyItem.dart';
import 'color/MyNetTool.dart';
class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
      ),
      home:ATHomeStatefulWidget(title: "首页",),
    );
  }
}
class ATHomeStatefulWidget extends BaseStatefulWidget{
  ATHomeStatefulWidget({Key key, this.title,this.male}) : super(key: key);
  final String title;
  final String male;
  @override
  State<StatefulWidget> createState() {
    return _MyHomeState();
  }
}
class _MyHomeState extends State<ATHomeStatefulWidget> with AutomaticKeepAliveClientMixin {
  var listData = <Male>[];
  var page     = 0;
  @override
  bool get wantKeepAlive => true;
  @override
  void initState(){
    super.initState();
    this.loadData();
  }
  Future <void>onRefresh() async {
    await Future.delayed(Duration(seconds: 2), (){
      page = 0;
      listData.clear();
      loadData();
    });
  }
  Future<void> loadData() async {
    MyNetTool tool = MyNetTool();
    Map<String ,dynamic> map = await MyNetTool.method(HttpMethod.Get, AppBaseUrl1, "/cats/lv2/statistics", null, null);
    this.listData.clear();
    Classify classif = Classify.fromJson(map);
    print(widget.male);
    var title = widget.male;
    if (title == "female"){
      this.listData.addAll(classif.female);
    }else if(title == "male"){
      this.listData.addAll(classif.male);
    }else if(title == "picture"){
      this.listData.addAll(classif.picture);
    }else{
      this.listData.addAll(classif.press);
    }
    setState(() {
    });
  }
  @override
  Widget build(BuildContext context) {
    final height = AppHeigh - AppToolBar - AppStatusBar - AppNaviBar;
    return Scaffold(

      body: Center(

        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            new Container(
              height: (height),
              width:  AppWidth,
              color: MyColor.AppFFFFFF,
              child: new RefreshIndicator(child: GridView.builder(
                padding: EdgeInsets.all(10.0),
                itemCount: this.listData.length,
                itemBuilder: (context,index){
                  Male model = this.listData[index];
                  var count = model.monthlyCount;
                  var url = model.bookCover.first;
                  var subTitle =  "月票 : $count";
                  return ATClassifyItem(title: model.name,subTitle: subTitle,url: MyUrl.getNewUrl(url),onTap: (){
                    Navigator.push(
                        context,
                        new MaterialPageRoute(
                            builder: (context) => new ATBookListStatefulWidget(title:model.name,major:widget.male,)));
                  });
                },
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisSpacing: 10,
                  mainAxisSpacing: 10,
                  crossAxisCount: 3,
                  childAspectRatio: 0.5,//宽高

                ),
              ), onRefresh: onRefresh),

            ),
          ],
        ),
      ),
    );
  }
}